from django.shortcuts import render
from django.http import JsonResponse
from .. import models

# Create your views here.
# 订单管理 列表页
def orderindex(request):
    # 获取所有用户
    uob = models.users.objects.all()
    for u in uob:
        # 获取同一个用户所有订单
        u.od = models.Order.objects.filter(uid=u).order_by('-id')
    # 分配数据
    context ={"data":uob}
    # 加载页面
    return render(request,'myadmin/order/index.html', context)

def orderedit(request):
    # 接收订单号 修改后状态
    oid = request.GET.get('oid')
    ezt =request.GET.get('ezt')
    # 状态名与状态编号对应关系
    dict1={"未付款":0,"待发货":1,"待收货":2,"待评价":3,"前台删除":4}
    # 获取订单对象
    ob = models.Order.objects.get(id=oid)
    # 修改订单状态
    ob.status = dict1[ezt]
    ob.save()
    return JsonResponse({"error":0,"msg":"修改成功"})

def orderinfo(request):
    print(request.GET.get('id'))
    # 接收订单号 获取订单对象
    ob = models.Order.objects.get(id=request.GET.get('id'))

    context ={"data":ob}
    # 加载页面
    return render(request,'myadmin/order/info.html',context)